//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AmericanPathPricer.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Payoff.h>
#include <gen/QL/Methods/Montecarlo/Path.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Methods::Montecarlo;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::CAmericanPathPricer (Cephei::QL::IPayoff^ payoff, UInt64 polynomOrder, QL::Methods::Montecarlo::LsmBasisSystem::PolynomTypeEnum polynomType) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CPayoff^ _Cpayoff;
    try
    {
#ifdef HANDLE
        _phAmericanPathPricer = NULL;
#endif
        _Cpayoff = safe_cast<CPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::Payoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::Payoff>&> (_Cpayoff->GetShared ()); 
        QuantLib::Size _polynomOrder = (QuantLib::Size)ValueHelper::Convert (polynomOrder); //d
        QuantLib::LsmBasisSystem::PolynomType _polynomType = (QuantLib::LsmBasisSystem::PolynomType)polynomType ;
        _ppAmericanPathPricer = new boost::shared_ptr<QuantLib::AmericanPathPricer> (new QuantLib::AmericanPathPricer ( _payoff,  _polynomOrder,  _polynomType ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::CAmericanPathPricer (boost::shared_ptr<QuantLib::AmericanPathPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPathPricer = NULL;
#endif
	_ppAmericanPathPricer = &childNative;
    
}
Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::CAmericanPathPricer (QuantLib::AmericanPathPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAmericanPathPricer = NULL;
#endif
	_ppAmericanPathPricer = new boost::shared_ptr<QuantLib::AmericanPathPricer> (&childNative);
    
    _AmericanPathPricerOwner = owner;
    
}

Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::CAmericanPathPricer (CAmericanPathPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPathPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAmericanPathPricer = new boost::shared_ptr<QuantLib::AmericanPathPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::CAmericanPathPricer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPathPricer = NULL;
#endif
	if (!t->IsSubclassOf(CAmericanPathPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::CAmericanPathPricer (QuantLib::Handle<QuantLib::AmericanPathPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanPathPricer = &childNative;
	_ppAmericanPathPricer = &static_cast<boost::shared_ptr<QuantLib::AmericanPathPricer>>(childNative.currentLink());
    
    _AmericanPathPricerOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::CAmericanPathPricer (QuantLib::Handle<QuantLib::AmericanPathPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanPathPricer = &childNative;
	_ppAmericanPathPricer = &static_cast<boost::shared_ptr<QuantLib::AmericanPathPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::CAmericanPathPricer (QuantLib::AmericanPathPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPathPricer = NULL;
#endif
	_ppAmericanPathPricer = new boost::shared_ptr<QuantLib::AmericanPathPricer> (new QuantLib::AmericanPathPricer (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::~CAmericanPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAmericanPathPricer != NULL)
    {
	    delete _ppAmericanPathPricer;
        _ppAmericanPathPricer = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::!CAmericanPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAmericanPathPricer != NULL)
    {
	    delete _ppAmericanPathPricer;
    }
}
QuantLib::AmericanPathPricer& Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::GetReference ()
{
    if (_ppAmericanPathPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppAmericanPathPricer;
}
boost::shared_ptr<QuantLib::AmericanPathPricer>& Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::GetShared ()
{
    if (_ppAmericanPathPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppAmericanPathPricer;
}
QuantLib::AmericanPathPricer* Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::GetPointer ()
{
    if (_ppAmericanPathPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppAmericanPathPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AmericanPathPricer>& Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::GetHandle ()
{
	if (_phAmericanPathPricer == NULL)
	{
		_phAmericanPathPricer = new Handle<QuantLib::AmericanPathPricer> (*_ppAmericanPathPricer);
	}
	return *_phAmericanPathPricer;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::HasNative () 
{
	return (_ppAmericanPathPricer != NULL);
}

Double Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer::State (Cephei::QL::Methods::Montecarlo::IPath^ path, UInt64 t)
{
    CPath^ _Cpath;
    try
    {
        _Cpath = safe_cast<CPath^> (path);
        _Cpath->Lock();
        QuantLib::Path& _path = static_cast<QuantLib::Path&> (_Cpath->GetReference ()); 
        QuantLib::Size _t = (QuantLib::Size)ValueHelper::Convert (t); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAmericanPathPricer)->state ( _path,  _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpath != nullptr) _Cpath->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IAmericanPathPricer^ Cephei::QL::Pricingengines::Vanilla::CAmericanPathPricer_Factory::Create (Cephei::QL::IPayoff^ payoff, UInt64 polynomOrder, QL::Methods::Montecarlo::LsmBasisSystem::PolynomTypeEnum polynomType)
{
    return REFNEW CAmericanPathPricer ( payoff,  polynomOrder,  polynomType);
}
